package com.sk.bilibili.view.activity;

import android.content.Context;
import android.content.Intent;

import androidx.viewpager.widget.PagerAdapter;

import com.sk.bilibili.adapter.pager.BaseFragmentAdapter;
import com.sk.bilibili.adapter.pager.BaseTypeFragmentAdapter;
import com.sk.bilibili.bean.Type;
import com.sk.bilibili.databinding.ActivityBaseTypeBinding;
import com.sk.bilibili.model.activity.BaseTypeModel;
import com.sk.bilibili.view.fragment.BaseTypeFragment;

import java.util.List;

public abstract class BaseTypeActivity extends BaseActivity<ActivityBaseTypeBinding, BaseTypeModel> {

    public static void start(Context context, Integer index,Class<?extends BaseTypeActivity> clazz){
        Intent intent = new Intent(context,clazz);
        intent.putExtra("index",index);
        context.startActivity(intent);
    }

    private Integer index;

    @Override
    protected void afterSetView() {
        index= (Integer) getIntent().getSerializableExtra("index");
        model.loadData();
        initView();
        bindEvent();
    }

    private void initView(){
        bind.tvTitle.setText(getTitleName());
    }

    protected abstract String getTitleName();

    // 只用于绑定公共事件
    private void bindEvent() {
        bind.ivBack.setOnClickListener(v->finish());
        bind.ivSearch.setOnClickListener(v->SearchActivity.start(this,null));
    }

    public void loadTypeSuccess(List<Type> types) {
        bind.vpShow.setAdapter(getFragmentAdapter(types));
        bind.tlShow.setupWithViewPager(bind.vpShow);
        bind.vpShow.setCurrentItem(index);
    }

    protected abstract BaseTypeFragmentAdapter getFragmentAdapter(List<Type> types);

}